package com.tsd.system.utils;

import com.alibaba.fastjson.JSON;
import com.tsd.core.utils.DeviceIdUtil;
import com.tsd.core.utils.FileUtil;
import com.tsd.core.utils.HlpUtils;
import com.tsd.core.utils.RSAUtil;
import com.tsd.core.vo.HlpException;
import com.tsd.core.vo.VerifyVo;
import com.tsd.core.datasource.DynamicDataSource;

import java.io.File;
import java.util.Base64;
import java.util.Date;

/**
 * 验证工具类
 *
 * @author Hillpool
 */
public class VerifyUtil {
    /**
     * 加载验证信息
     *
     * @return
     */
    static private VerifyVo loadVerifyVo() {
        try {
//            String root = "D:\\work\\project\\2021\\svn-tsd\\code-ma\\api\\common\\src\\main\\resources";
            String root = HlpUtils.getClassPath();
            String fileName = new File(root, "tsd_license.json").getAbsolutePath();
            String json = FileUtil.readToString(fileName);
            return JSON.parseObject(json, VerifyVo.class);
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * 执行验证
     *
     * @param vo
     * @return
     * @throws HlpException
     */
    static private void doVerify(VerifyVo vo) throws HlpException {
        try {
            StringBuilder buffer = new StringBuilder();
            buffer.append(vo.getProductString()).append(",");
            buffer.append(vo.getAccount_set_qty()).append(",");
            buffer.append(vo.getExpiry_date()).append(",");
            buffer.append(vo.getSerial_number());
            RSAUtil rsaUtil = new RSAUtil();
            rsaUtil.setPublicK(vo.getVerify_key());
            String decoded = rsaUtil.decryptByPublieKey(vo.getVerify_text());
            String str = new String(Base64.getDecoder().decode(decoded));
            String sn = DeviceIdUtil.getCpuSerial();
            if (!HlpUtils.isEqual(sn, vo.getSerial_number())) {
                System.out.println("当前设备标识是：" + sn);
                throw new HlpException("请合法授权的设备上使用本系统，https://www.hillpool.cn");
            }
            if (!buffer.toString().equals(str)) {
                throw new HlpException("请获取合法授权使用本系统，https://www.hillpool.cn");
            }
        } catch (Exception e) {
            if (e instanceof HlpException) {
                throw (HlpException) e;
            } else {
                e.printStackTrace();
                throw new HlpException("验证授权失败！");
            }
        }
    }

    static public void verify() throws HlpException {
        DynamicDataSource.ok = false;
        VerifyVo vo = loadVerifyVo();
        doVerify(vo);
        try {
            Date exp = HlpUtils.string2Date("" + vo.getExpiry_date(), "yyyyMMdd");
            if (exp.before(new Date())) {
                System.out.println("授权已过期，请联系官网客服：https://www.hillpool.cn");
            } else {
                DynamicDataSource.ok = true;
            }
        } catch (Exception e) {
            throw new HlpException("验证授权失败！");
        }
    }

    static public void main(String[] args) {
        try {
            verify();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
